package com.baohua.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.baohua.dao.MemberDao;
import com.baohua.exception.BaohuaSystemException;
import com.baohua.model.Member;
import com.baohua.model.Staff;
import com.baohua.service.MemberService;
import com.baohua.util.BeanUtils;

@Service("memberService")
public class MemberServiceImpl implements MemberService {

	private MemberDao memberDao;

	public MemberDao getMemberDao() {
		return memberDao;
	}

	@Resource(name = "memberDao")
	public void setMemberDao(MemberDao memberDao) {
		this.memberDao = memberDao;
	}

	/**
	 * 查询会员列表
	 * 
	 * @return
	 */
	@Override
	public List<Member> queryMember(String sql, int page, int pageSize) {
		return memberDao.queryMember(sql, page, pageSize);
	}

	/**
	 * 根据会员编号查询会员
	 * 
	 * @param memberNo
	 * @return
	 */
	@Override
	public Member queryMemberByMemberNo(String memberNo)
			throws BaohuaSystemException {
		if (memberNo == null) {
			throw new BaohuaSystemException("会员编号不能为空");
		} else {
			return memberDao.queryMemberBymenberNo(memberNo);
		}
	}

	/**
	 * 根据会员姓名查询会员
	 * 
	 * @param memberName
	 * @return
	 */
	@Override
	public Member queryMemberBymemberName(String memberName)
			throws BaohuaSystemException {
		if (memberName == null) {
			throw new BaohuaSystemException("会员名不能为空");
		} else {
			return memberDao.queryMemberByMemberName(memberName);
		}
	}

	/**
	 * 增加会员
	 * 
	 * @param member
	 */
	@Override
	public void addMember(Member member) throws BaohuaSystemException {
		Member m = memberDao.queryMemberBymenberNo(member.getMemberNo());
		if (m == null) {
			memberDao.addMember(member);
		} else {
			throw new BaohuaSystemException("会员已存在");
		}
	}

	/**
	 * 删除会员
	 * 
	 * @param member
	 */
	@Override
	public void delMember(int memberId) throws BaohuaSystemException {
		Member m = memberDao.queryMemberByMemberId(memberId);
		if (m != null) {
			memberDao.delMember(m);
		} else {
			throw new BaohuaSystemException("会员不存在");
		}
	}

	/**
	 * 修改会员
	 * 
	 * @param member
	 */
	@Override
	public void updateMember(Member member) throws BaohuaSystemException {
		Member m = memberDao.queryMemberByMemberId(member.getMemberId());
		if (m != null) {
			if (!BeanUtils.compareObject(m, member)) {
				// BeanUtils不拷贝集合类
				BeanUtils.copyProperty(member, m);
				m.setMemberId(member.getMemberId());
				memberDao.updateMember(m);
			}
		} else {
			throw new BaohuaSystemException("会员不存在");
		}
	}

	/**
	 * 统计会员数
	 * 
	 * @return
	 */
	@Override
	public long countMember(String sql) {
		return memberDao.countMember(sql);
	}

	/**
	 * 根据会员Id进行查询
	 * 
	 * @param MemberId
	 * @return
	 * @throws BaohuaSystemException
	 */
	@Override
	public Member queryMemberById(int memberId) throws BaohuaSystemException {
		Member m = memberDao.queryMemberByMemberId(memberId);
		if (m == null) {
			throw new BaohuaSystemException("会员不存在");
		} else {
			return m;
		}
	}

}
